﻿-----------them chi tiet don hang ----------------
CREATE
--ALTER
PROC proc_ThemCTDonHang
@madh varchar(6),@mama varchar(6),@soluong int,@res int output
AS
BEGIN TRAN
	BEGIN TRY
		if(@soluong <= (Select SLMA From MONAN where @mama = MaMA))
		begin
			Insert into CTDONHANG(MaDH,MaMA,SL) values (@madh,@mama,@soluong)
			set @res = 1
		end
		else
			set @res = 0		
	END TRY
	BEGIN CATCH
		--RAISERROR (N'LỖI HỆ THỐNG',16,1)
		--RAISERROR 
		
		declare @ErrorMsg varchar(2000)
		select @ErrorMsg = N'Lỗi: ' + ERROR_MESSAGE()
		raiserror(@ErrorMsg, 16,1)
		set @res = 0
		ROLLBACK TRAN
		RETURN
	END CATCH
COMMIT TRAN
GO

declare  @res int
exec proc_ThemCTDonHang 'DH001','MA002',5,@res output
print @res

select * From CTDONHANG
select * From DONHANG
select * from LICHGIAO
select * from KhachHang where makh='kh001'